import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		loginInfo: null,
		userInfo: null,
		newMessage: [],
	},
	getters: {
		isLogin: (state) => {
			return state.loginInfo ? true : false
		}
	},
	mutations: {
		updateLoginInfo(state, info) {
			state.loginInfo = info
		},
		updateUserInfo(state, info) {
			state.userInfo = info
		},
		logout(state) {
			state.loginInfo = null
			state.userInfo = null
		},
		addNewMessage(state, msg) {
			// console.log('最新消息：', msg)
			state.newMessage.push(JSON.parse(msg))
		},
		clearNewMessage(state) {
			state.newMessage = []
		}
	},
	actions: {
		async reloadLoginInfoFromLocal ({ commit, state }, params) {
			let loginInfo = uni.getStorageSync('loginInfo')
			if(!loginInfo || (new Date().valueOf()) > loginInfo.expiresAt){
				loginInfo = null
				uni.removeStorageSync('loginInfo')
			}
			console.log('登录信息:', loginInfo);
			commit('updateLoginInfo', loginInfo)
		},
		async addNewMessage({ commit, state }, params) {
			commit('addNewMessage', params)
		},
		async clearNewMessage({ commit, state }) {
			commit('clearNewMessage')
		},
		updateTokenAction ({ commit, state }, params) {
			console.log('更新token', params)
			uni.setStorageSync('loginInfo', {
				...params
			})
			commit('updateLoginInfo', params)
		},
		async loginAction ({ commit, state }, params) {
			const res = await Vue.prototype.$u.api.getLogin(params)
			console.log('登录结果：', res)
			if (res) {
				uni.setStorageSync('loginInfo', {
					...res.data
				})
				commit('updateLoginInfo', res.data)
				return true
			} else {
				return false
			}
		},
		async noLoginUpdateLoginInfo({ commit, state }, data) {
			// if(!state.appCacheWxLoginInfo)return
			uni.setStorageSync('loginInfo', {
				...data
			})
			commit('updateLoginInfo', data)
		},
		async refreshToken({ commit, state }, params) {
			// 判断存储的数据什么时候过期，在过期前6个月刷新token
			const loginInfo = uni.getStorageSync('loginInfo')
			const expiresAt = loginInfo.expiresAt
			const timeNow = new Date().valueOf()
			const expiresTime = Math.ceil(((expiresAt-timeNow)/1000)/86400)
			console.log('还剩多少天过期:', expiresTime)
			if (expiresTime <= 180) {
				const tokenRes = await Vue.prototype.$u.api.refreshToken()
				console.log('刷新token:', tokenRes)
				uni.setStorageSync('loginInfo', {
					...tokenRes.data
				})
				commit('updateLoginInfo', tokenRes.data)
			} else {
				console.log('loginInfo:', loginInfo)
				commit('updateLoginInfo', loginInfo)
			}
		},
		async updateUserInfo ({ commit, state, getters }) {
			if(!getters.isLogin){
				uni.showToast({
					title: '用户尚未登录',
					icon: 'none',
				})
				return
			}
			const res = await Vue.prototype.$u.api.userInfo()
			commit('updateUserInfo', res.data)
		},
		async logout({ commit, state, getters }, callApi = true){
			if(callApi) await Vue.prototype.$u.api.logout()
			commit('logout')
			uni.removeStorageSync('loginInfo')
			uni.removeStorageSync(Vue.prototype.$config.friends_list_key)
			uni.removeStorageSync(Vue.prototype.$config.group_list_key)
		}
	}
})